package com.lingshi.shopping_manager_api.controller;

import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.FileService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件管理
 */
@RestController
@RequestMapping("/file")
public class FileController {


    @DubboReference
    private FileService fileService;

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadImage")
    public BaseResult upload(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        byte[] bytes = file.getBytes();

        String filePath = fileService.uploadImage(bytes, originalFilename);

        return BaseResult.success(filePath);
    }

    /**
     * 删除文件
     * @param filePath
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(String filePath) {
        fileService.delete(filePath);
       return BaseResult.success();
    }
}
